//
//  TimeModel.m
//  YunOs
//
//  Created by 周斌 on 15/11/9.
//  Copyright © 2015年 BruchChou. All rights reserved.
//

#import "TimeModel.h"

@implementation TimeModel
+(NSDate *)getTimeIntervalWithFistDayOfYear:(NSInteger)year
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [dateFormatter dateFromString:[NSString stringWithFormat:@"%ld-01-01 00:00:00",(long)year]];
}
+(NSDate*)dateFormYearString:(NSString *)yearStr{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM"];
    return [dateFormatter dateFromString:yearStr];
}
+(NSString *)getNextDayTime:(NSString *)timeStr {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:timeStr];
    NSDate *nextDay = [NSDate dateWithTimeInterval:24*60*60 sinceDate:date];//后一天
    NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
    [dateFormatter2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [dateFormatter stringFromDate:nextDay];
}
+(NSDate *)dateFromString:(NSString *)dateStr
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    return [dateFormatter dateFromString:dateStr];
}
+(NSString *)strFromTimeInterval:(long long)timeInterval
{
    NSDate *date=[NSDate dateWithTimeIntervalSince1970:timeInterval+8*60*60];//把北京的时区差计算进去
    NSDateComponents *comps=[self getComps:date];
    return [NSString stringWithFormat:@"%ld-%@-%@",(long)[comps year],[self strMonthFromIntMonth:[comps month]],[self strDayFromIntDay:[comps day]]];
}

+(NSDateComponents *)getComps:(NSDate *)date
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay | NSCalendarUnitWeekday|NSCalendarUnitHour
                              |NSCalendarUnitMinute  fromDate:date];
    return comps;
}
+(NSInteger)getCurrentYear
{
    NSDateComponents *comps=[self getComps:[NSDate date]];
    return [comps year];
}
+(NSInteger)getCurrentMonth
{
    NSDateComponents *comps=[self getComps:[NSDate date]];
    return [comps month];
}
+(NSInteger)getCurrentDay
{
    NSDateComponents *comps=[self getComps:[NSDate date]];
    return [comps day];
}
+(NSInteger)getCurrentHour{
    NSDateComponents *comps = [self getComps:[NSDate date]];
    return [comps hour];
}
+(NSInteger)getCurrentMintue{
    NSDateComponents *comps = [self getComps:[NSDate date]];
    return [comps minute];
}

+(NSInteger)getCurrentWeekday
{
    NSDateComponents *comps=[self getComps:[NSDate date]];
    return [comps weekday];
}


+(NSInteger)getDayCount:(NSString *)dateStr
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM"];
    NSDate *date = [dateFormatter dateFromString:dateStr];
    return [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date].length;
}

+(NSString *)strMonthFromIntMonth:(NSInteger)intMonth
{
    return intMonth>9?[NSString stringWithFormat:@"%ld",(long)intMonth]:[NSString stringWithFormat:@"0%ld",(long)intMonth];
}

+(NSString *)strDayFromIntDay:(NSInteger)intDay
{
    return intDay > 9 ? [NSString stringWithFormat:@"%ld",(long)intDay]:[NSString stringWithFormat:@"0%ld",(long)intDay];
}

+(NSString *)commonStrFromTimeInterval:(long long)timeInterval
{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];//把北京的时区差计算进去
    NSDateComponents *comps = [self getComps:date];
    return [NSString stringWithFormat:@"%ld %@ %@",(long)[comps year],[self strMonthFromIntMonth:[comps month]],[self strDayFromIntDay:[comps day]]];
}

+(NSString *)getCurrentTimeFormatter:(NSString *)formatter{
    
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterFullStyle];
    [dateFormatter setDateFormat:formatter];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    return dateString;
}

+(NSString *)getScheduledTimeWithTimeStr:(NSString *)timeStr inFormatter:(NSString *)inFormatter outFormatter:(NSString *)outFormatter {
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterFullStyle];
    [dateFormatter setDateFormat:outFormatter];
    NSString *dateString = [dateFormatter stringFromDate:[self dateFromString:timeStr andFormatter:inFormatter]];
    return dateString;
}

+(NSString *)getCurrentDayTime{
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterFullStyle];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    return dateString;
}

+(NSString *)getNextDayTime {
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:4];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:24*60*60 sinceDate:[NSDate date]]];
    return dateString;
}
+ (NSString *) getCurrentWeekTime {
    NSDate *nowDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:nowDate];
    // 获取今天是周几
    NSInteger weekDay = [comp weekday];
    // 获取几天是几号
    NSInteger day = [comp day];
    // 计算当前日期和本周的星期一和星期天相差天数
    long firstDiff,lastDiff;
    //    weekDay = 1;
    if (weekDay == 1){
        firstDiff = -6;
        lastDiff = 0;
    }else{
        firstDiff = [calendar firstWeekday] - weekDay + 1;
        lastDiff = 8 - weekDay;
    }
    NSLog(@"firstDiff: %ld   lastDiff: %ld",firstDiff,lastDiff);
    
    // 在当前日期(去掉时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:nowDate];
    [firstDayComp setDay:day + firstDiff];
    NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
    
    NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay | NSCalendarUnitWeekday   fromDate:nowDate];
    [lastDayComp setDay:day + lastDiff];
    NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];
    NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];
    return  [NSString stringWithFormat:@"%@ 至 %@",firstDay,lastDay];
}
+ (NSString *) getCurrentTime
{
//    NSDate *now = [NSDate date];
//
//
//    NSCalendar *calendar = [NSCalendar currentCalendar];
//    NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
//    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
//
//    int year = [dateComponent year];
//    int month = [dateComponent month];
//    int day = [dateComponent day];
//    int hour = [dateComponent hour];
//    int minute = [dateComponent minute];
//    int second = [dateComponent second];
//
//    return [NSString stringWithFormat:@"%d-%d-%d %d:%d:%d",year,month,day,hour,minute,second];
    
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:4];
    [dateFormatter setDateFormat:@"yyyy MM dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    return dateString;

}
+(NSString *)getCurrentTimeWithTimeStr:(NSString *)timeStr{
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:4];
    [dateFormatter setDateFormat:timeStr];
    NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
    return dateString;
}

+(NSString *)getDateStringWithTimeInterval:(long long)timeInterval{
    NSDate *d =[[NSDate alloc] initWithTimeIntervalSince1970:timeInterval/1000.0];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString*timeString=[formatter stringFromDate:d];
    return timeString;
}

+(NSString *)getDateStringWithTimeInterval:(long long)timeInterval withFormateString:(NSString *)formateString{
    NSDate *d =[[NSDate alloc] initWithTimeIntervalSince1970:timeInterval/1000.0];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:formateString];
    NSString*timeString=[formatter stringFromDate:d];
    return timeString;
}
+(NSString *)getTimeStringWithTimeInterval:(long long)timeInterval{
    NSDate *d =[[NSDate alloc] initWithTimeIntervalSince1970:timeInterval/1000.0];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"HH:mm"];
    NSString*timeString=[formatter stringFromDate:d];
    return timeString;
}
+(NSString *)getWeekDayStringWithTimeInterval:(long long)timeInterval{
    NSDate *d =[[NSDate alloc] initWithTimeIntervalSince1970:timeInterval/1000.0];
    NSDateComponents *componets= [[NSCalendar autoupdatingCurrentCalendar] components:NSCalendarUnitWeekday fromDate:d];
    NSInteger weekday = [componets weekday];
    NSArray *weekArray = @[@"星期日",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六"];
    NSString *weekStr = weekArray[weekday-1];
    return weekStr;
}
+ (NSString *)messageDate:(long)dateString{
    //  http://www.jianshu.com/p/c04a953d3b7e
    NSString *nowDateStr = [self getDateStringWithTimeInterval:dateString withFormateString:@"yyyy-MM-dd HH:mm:ss"];
    // ------实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//这里的格式必须和DateString格式一致
    
    NSDate * nowDate = [NSDate date];
    
    // ------将需要转换的时间转换成 NSDate 对象
    NSDate * needFormatDate = [dateFormatter dateFromString:nowDateStr];
    
    // ------取当前时间和转换时间两个日期对象的时间间隔
    NSTimeInterval time = [nowDate timeIntervalSinceDate:needFormatDate];
    
    // ------再然后，把间隔的秒数折算成天数和小时数：
    
    NSString *dateStr = [[NSString alloc] init];
    
//    if (time<=60) {  //1分钟以内的
//        
//        dateStr = @"刚刚";
//        
//    }else if(time<=60*60){  //一个小时以内的
//        
//        int mins = time/60;
//        dateStr = [NSString stringWithFormat:@"%d分钟前",mins];
//        
//    }else
        if(time<=60*60*24){  //在两天内的
        
        [dateFormatter setDateFormat:@"YYYY-MM-dd"];
        NSString * need_yMd = [dateFormatter stringFromDate:needFormatDate];
        NSString *now_yMd = [dateFormatter stringFromDate:nowDate];
        
        [dateFormatter setDateFormat:@"HH:mm"];
        if ([need_yMd isEqualToString:now_yMd]) {
            //在同一天
            dateStr = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:needFormatDate]];//
        }else{
            //昨天
            dateStr = [NSString stringWithFormat:@"昨天"];//[dateFormatter stringFromDate:needFormatDate]
        }
    }else {
        
        [dateFormatter setDateFormat:@"yyyy"];
        NSString * yearStr = [dateFormatter stringFromDate:needFormatDate];
        NSString *nowYear = [dateFormatter stringFromDate:nowDate];
        
        if ([yearStr isEqualToString:nowYear]) {
            //在同一年
            [dateFormatter setDateFormat:@"MM-dd"];
            dateStr = [dateFormatter stringFromDate:needFormatDate];
        }else{
            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
            dateStr = [dateFormatter stringFromDate:needFormatDate];
        }
    }
    
    return dateStr;
    
    
    
//    //实例化一个NSDateFormatter对象
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    [dateFormatter setDateFormat:formate];
//    
//    NSDate * nowDate = [NSDate date];
//    
//    //  将需要转换的时间转换成 NSDate 对象
//    NSDate * needFormatDate = [dateFormatter dateFromString:dateString];
//    //  取当前时间和转换时间两个日期对象的时间间隔
//    //  这里的NSTimeInterval 并不是对象，是基本型，其实是double类型，是由c定义的:  typedef double NSTimeInterval;
//    NSTimeInterval time = [nowDate timeIntervalSinceDate:needFormatDate];
//    
//    // 再然后，把间隔的秒数折算成天数和小时数：
//    
//    NSString *dateStr = @"";
//    if(time<=60*60*24){ // 在两天内的
//        
//        [dateFormatter setDateFormat:@"YYYY-MM-dd"];
//        NSString * need_yMd = [dateFormatter stringFromDate:needFormatDate];
//        NSString *now_yMd = [dateFormatter stringFromDate:nowDate];
//        
//        [dateFormatter setDateFormat:@"HH:mm"];
//        if ([need_yMd isEqualToString:now_yMd]) {
//            // 在同一天
//            dateStr = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:needFormatDate]];
//        }else{
//            //  昨天
//            dateStr = [NSString stringWithFormat:@"昨天 %@",[dateFormatter stringFromDate:needFormatDate]];
//        }
//    }else {
//        
////        [dateFormatter setDateFormat:@"yyyy"];
////        NSString * yearStr = [dateFormatter stringFromDate:needFormatDate];
////        NSString *nowYear = [dateFormatter stringFromDate:nowDate];
//        
////        if ([yearStr isEqualToString:nowYear]) {
////            //  在同一年
////            [dateFormatter setDateFormat:@"MM-dd"];
////            dateStr = [dateFormatter stringFromDate:needFormatDate];
////        }else{
//            [dateFormatter setDateFormat:@"yyyy-MM-dd"];
//            dateStr = [dateFormatter stringFromDate:needFormatDate];
////        }
//    }
//    
//    return dateStr;
}
/**
 //  和当前时间比较
 //   1）1分钟以内 显示        :    刚刚
 //   2）1小时以内 显示        :    X分钟前
 //    3）今天或者昨天 显示      :    今天 09:30   昨天 09:30
 //    4) 今年显示              :   09月12日
 //    5) 大于本年      显示    :    2013/09/09
**/

+ (NSString *)formateDate:(NSString *)dateString withFormate:(NSString *)formate
{
    
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formate];
    
    NSDate * nowDate = [NSDate date];
    
    //  将需要转换的时间转换成 NSDate 对象
    NSDate * needFormatDate = [dateFormatter dateFromString:dateString];
    //  取当前时间和转换时间两个日期对象的时间间隔
    //  这里的NSTimeInterval 并不是对象，是基本型，其实是double类型，是由c定义的:  typedef double NSTimeInterval;
    NSTimeInterval time = [nowDate timeIntervalSinceDate:needFormatDate];
    
    // 再然后，把间隔的秒数折算成天数和小时数：
    
    NSString *dateStr = @"";
    
    if (time<=60) {  // 1分钟以内的
        dateStr = @"刚刚";
    }else if(time<=60*60){ //  一个小时以内的
        
        int mins = time/60;
        dateStr = [NSString stringWithFormat:@"%d分钟前",mins];
        
    }else if(time<=60*60*24){ // 在两天内的
        
        [dateFormatter setDateFormat:@"YYYY/MM/dd"];
        NSString * need_yMd = [dateFormatter stringFromDate:needFormatDate];
        NSString *now_yMd = [dateFormatter stringFromDate:nowDate];
        
        [dateFormatter setDateFormat:@"HH:mm"];
        if ([need_yMd isEqualToString:now_yMd]) {
            // 在同一天
            dateStr = [NSString stringWithFormat:@"今天 %@",[dateFormatter stringFromDate:needFormatDate]];
        }else{
            //  昨天
            dateStr = [NSString stringWithFormat:@"昨天 %@",[dateFormatter stringFromDate:needFormatDate]];
        }
    }else {
        
        [dateFormatter setDateFormat:@"yyyy"];
        NSString * yearStr = [dateFormatter stringFromDate:needFormatDate];
        NSString *nowYear = [dateFormatter stringFromDate:nowDate];
        
        if ([yearStr isEqualToString:nowYear]) {
            //  在同一年
            [dateFormatter setDateFormat:@"MM月dd日"];
            dateStr = [dateFormatter stringFromDate:needFormatDate];
        }else{
            [dateFormatter setDateFormat:@"yyyy/MM/dd"];
            dateStr = [dateFormatter stringFromDate:needFormatDate];
        }
    }
    
    return dateStr;
}
+ (NSString *)withDateString:(NSString *)dateString formateString:(NSString *)formateString
{
    
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    if ([dateString componentsSeparatedByString:@":"].count==3) {
        dateString =[NSString stringWithFormat:@"%@:%@",[dateString componentsSeparatedByString:@":"][0],[dateString componentsSeparatedByString:@":"][1]];
    }
    [dateFormatter setDateFormat:formateString];
    
    NSDate * nowDate = [NSDate date];
    
    //  将需要转换的时间转换成 NSDate 对象
    NSDate * needFormatDate = [dateFormatter dateFromString:dateString];
    //  取当前时间和转换时间两个日期对象的时间间隔
    //  这里的NSTimeInterval 并不是对象，是基本型，其实是double类型，是由c定义的:  typedef double NSTimeInterval;
    NSTimeInterval time = [nowDate timeIntervalSinceDate:needFormatDate];
    
    // 再然后，把间隔的秒数折算成天数和小时数：
    
    NSString *dateStr = @"";
    
    if(time<=60*60*24){ // 在两天内的
        [dateFormatter setDateFormat:@"YYYY/MM/dd"];
        NSString * need_yMd = [dateFormatter stringFromDate:needFormatDate];
        NSString *now_yMd = [dateFormatter stringFromDate:nowDate];
        
        [dateFormatter setDateFormat:@"HH:mm"];
        if ([need_yMd isEqualToString:now_yMd]) {
            // 在同一天
            dateStr = [dateFormatter stringFromDate:needFormatDate];
        }else{
            //  昨天
            dateStr = [NSString stringWithFormat:@"昨天 %@",[dateFormatter stringFromDate:needFormatDate]];
        }
    }else {
        
        [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm"];
        dateStr = [dateFormatter stringFromDate:needFormatDate];
        
    }
    
    return dateStr;
}

+ (long long)timeIntervalFromDate:(NSString *)dateStr withformatter:(NSString *)formatterStr{
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    
    [formatter setDateFormat:formatterStr];
    
    NSDate * date = [formatter dateFromString:dateStr];
    
    return [date timeIntervalSince1970]*1000;
}

+ (long long)timeSecondIntervalFromDate:(NSString *)dateStr withformatter:(NSString *)formatterStr{
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    
    [formatter setDateFormat:formatterStr];
    
    NSDate * date = [formatter dateFromString:dateStr];
    
    return [date timeIntervalSince1970];
}

+ (long long)longlongFromDate:(NSDate *)date
{
    return (long long)[date timeIntervalSince1970] * 1000;
}

+ (NSString *)dateFromLongLong:(long long)msSince1970 andFormatter:(NSString *)formatter
{
    NSString *dateString = [self stringFromDate:[NSDate dateWithTimeIntervalSince1970:msSince1970 / 1000] andFormatter:formatter];
    return dateString;
}

+ (NSString *)dateFromLongLongSecond:(long long)msSince1970 andFormatter:(NSString *)formatter
{
    NSString *dateString = [self stringFromDate:[NSDate dateWithTimeIntervalSince1970:msSince1970] andFormatter:formatter];
    return dateString;
}

+ (NSString *)stringFromDate:(NSDate *)date andFormatter:(NSString *)formatter{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatter];
    NSString *destDateString = [dateFormatter stringFromDate:date];
    return destDateString;
    
}

+(NSDate *)dateFromString:(NSString *)dateStr andFormatter:(NSString *)formatter{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatter];
    return [dateFormatter dateFromString:dateStr];
}

+(NSString*)getDateWithYear:(int)year month:(int)month day:(int)day withDate:(NSDate *)date andFormatter:(NSString *)formatterStr
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *comps = nil;
    comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
    NSDateComponents *adcomps = [[NSDateComponents alloc] init];
    [adcomps setYear:year];
    [adcomps setMonth:month];
    [adcomps setDay:day];
    NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:date options:0];
    NSDateFormatter *formatter =  [[NSDateFormatter alloc] init];
    [formatter setDateFormat:formatterStr];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/beijing"];
    [formatter setTimeZone:timeZone];
    NSString *dateFromData = [formatter stringFromDate:newdate];

    return dateFromData;
}


/**
 
 *  获取网络当前时间
 
 */

+ (NSString *)getInternetDate

{
    
    NSString *urlString = @"https://m.baidu.com";
    
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    
    // 实例化NSMutableURLRequest，并进行参数配置
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    
    [request setURL:[NSURL URLWithString: urlString]];
    
    [request setCachePolicy:NSURLRequestReloadIgnoringCacheData];
    
    [request setTimeoutInterval: 2];
    
    [request setHTTPShouldHandleCookies:FALSE];
    
    [request setHTTPMethod:@"GET"];
    
    NSError *error = nil;
    
    NSHTTPURLResponse *response;
    
    [NSURLConnection sendSynchronousRequest:request
     
                          returningResponse:&response error:&error];
    
    
    
    // 处理返回的数据
    
    //NSString *strReturn = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    if (error) {
        return [dateFormatter stringFromDate:[NSDate date]];
    }
    
    NSString *date = [[response allHeaderFields] objectForKey:@"Date"];
    
    date = [date substringFromIndex:5];
    
    date = [date substringToIndex:[date length]-4];
    
    NSDateFormatter *dMatter = [[NSDateFormatter alloc] init];
    
    dMatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_Zh"];
    
    [dMatter setDateFormat:@"dd MMM yyyy HH:mm:ss"];
    
    NSDate *netDate = [[dMatter dateFromString:date] dateByAddingTimeInterval:60*60*8];
    
    NSString *destDateString = [dateFormatter stringFromDate:netDate];
    
    return destDateString;
    
}

#pragma mark  上篇与下篇
+(NSString *)getLastDayFormatter:(NSString *)dayTimeStr upPage:(BOOL)isUpPage{
    NSDate *lastDay;
    NSDate *date = [NSDate date];
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    date =  [dateFormatter dateFromString:dayTimeStr];
    if (isUpPage) {
       lastDay = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:date];//前一天
    } else {
       lastDay = [NSDate dateWithTimeInterval:24*60*60 sinceDate:date];//后一天
    }
    //NSDate转NSString
     NSString *currentDateString = [dateFormatter stringFromDate:lastDay];
    
    return currentDateString;
}

+ (NSString *)getLastWeekFormatter:(NSString *)weekTimeStr upPage:(BOOL)isUpPage{
    NSDate * date = [NSDate date];
    NSDate * nowDate = [NSDate date];
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    date =  [dateFormatter dateFromString:[weekTimeStr componentsSeparatedByString:@"至"][0]];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *comp1 = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:date];

    if (isUpPage) {
        [comp1 setDay:([comp1 day] - 7)];
        nowDate = [calendar dateFromComponents:comp1];
    } else {
        [comp1 setDay:([comp1 day] + 7)];
        nowDate = [calendar dateFromComponents:comp1];
    }
    NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:nowDate];
    
    // 获取今天是周几
    NSInteger weekDay = [comp weekday];
    // 获取几天是几号
    NSInteger day = [comp day];
    // 计算当前日期和本周的星期一和星期天相差天数
    long firstDiff,lastDiff;
    //    weekDay = 1;
    if (weekDay == 1){
        firstDiff = -6;
        lastDiff = 0;
    }else{
        firstDiff = [calendar firstWeekday] - weekDay + 1;
        lastDiff = 8 - weekDay;
    }
    NSLog(@"firstDiff: %ld   lastDiff: %ld",firstDiff,lastDiff);
    
    // 在当前日期(去掉时分秒)基础上加上差的天数
    NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay | NSCalendarUnitWeekday fromDate:nowDate];
    [firstDayComp setDay:day + firstDiff];
    NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];
    
    NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay | NSCalendarUnitWeekday   fromDate:nowDate];
    [lastDayComp setDay:day + lastDiff];
    NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];
    NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];
    return  [NSString stringWithFormat:@"%@ 至 %@",firstDay,lastDay];
    
//    return weekStr;
}

+(NSString *)getLastMonthFormatter:(NSString *)monthTimeStr upPage:(BOOL)isUpPage{
    NSDate *lastDay;
    NSDate *date = [NSDate date];
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];//创建一个日期格式化器
    [dateFormatter setDateFormat:@"yyyy-MM"];
    date =  [dateFormatter dateFromString:monthTimeStr];
     NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    if (isUpPage) {
        NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init];
        [lastMonthComps setMonth:-1];
        lastDay = [calendar dateByAddingComponents:lastMonthComps toDate:date options:0];
    } else {
        NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init];
        [lastMonthComps setMonth:1];
        lastDay = [calendar dateByAddingComponents:lastMonthComps toDate:date options:0];
    }
    //NSDate转NSString
    NSString *currentDateString = [dateFormatter stringFromDate:lastDay];
    
    return currentDateString;
    
}

/**
 
 * 开始到结束的时间差
 
 */

+ (NSString *)dateTimeDifferenceWithStartTime:(NSString *)startTime endTime:(NSString *)endTime{
    
    NSDateFormatter *date = [[NSDateFormatter alloc]init];
    
    [date setDateFormat:@"yyyy-MM-dd HH:mm"];
    
    NSDate *startD =[date dateFromString:startTime];
    
    NSDate *endD = [date dateFromString:endTime];
    
    NSTimeInterval start = [startD timeIntervalSince1970]*1;
    
    NSTimeInterval end = [endD timeIntervalSince1970]*1;
    
    NSTimeInterval value = end - start;
    
    int hour = (int)value / (60 *60);
    
    int ridOfHour = (int)value % (60 *60);
    
    double minute = (double)ridOfHour /(60 *60);
    
    double cacluTime = (double)hour + (double)minute;
        
    NSString *str = [NSString stringWithFormat:@"%.2f",cacluTime];
    
    return str;
}

+ (NSTimeInterval)pleaseInsertStarTime:(NSString *)starTime andInsertEndTime:(NSString *)endTime {
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//根据自己的需求定义格式
    NSDate* startDate = [formater dateFromString:starTime];
    NSDate* endDate = [formater dateFromString:endTime];
    NSTimeInterval time = [endDate timeIntervalSinceDate:startDate];
    return time;
}

+ (NSTimeInterval)pleaseInsertStarTimeInterval:(NSString *)starTime {
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//根据自己的需求定义格式
    NSDate* startDate = [formater dateFromString:starTime];
    NSDate* endDate = [NSDate date];
    NSTimeInterval time = [endDate timeIntervalSinceDate:startDate];
    return time;
}

+ (NSString *)getRidOfSpaceAndTab: (NSString*)contentStr{
    NSString *noSpaceStr;
    noSpaceStr = [contentStr
                  stringByReplacingOccurrencesOfString:@"\r"
                  withString:@""];
    
    noSpaceStr = [noSpaceStr
                  stringByReplacingOccurrencesOfString:@"\n"
                  withString:@""];
    
    return noSpaceStr;
}


+ (NSString *)getRidOfSecondStr:(NSString*)timeStr {
    NSString *returnStr = [NSString stringWithFormat:@"%@:%@",[timeStr componentsSeparatedByString:@":"][0],[timeStr componentsSeparatedByString:@":"][1]];
    return returnStr;
}

//根据格式转换时间戳
+ (NSString *)getTimeFromTimeInterval:(NSString *)timeInterval FromFormatter:(NSString *)formatter {
    // iOS 生成的时间戳是10位
    NSTimeInterval interval    =[timeInterval doubleValue] / 1000.0;
    NSDate *date               = [NSDate dateWithTimeIntervalSince1970:interval];
    NSDateFormatter *matter = [[NSDateFormatter alloc] init];
    [matter setDateFormat:formatter];
    NSString *dateString = [matter stringFromDate:date];
    return dateString;
}
@end
